<template>
  <div id="tablebox" class="rightBodyContent">
    <div class="rightBodyContent-in">
      <p class="titlefoodTitle"><span>当前账号：<i
          class="name">{{ userInfo ? userInfo.realname : '---' }}</i>，班级人数：<i
          class="name">{{ countClassNumber }}</i>, 当天评价人数：<i class="numberUse">{{ countUser }}</i></span></p>
      <el-button size="mini" class="ml-10">刷新</el-button>
    </div>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
export default {
  data() {
    return {
      showCommont: [],
      oldData: [],
      showId: null,
      flag: true,
      countUser: 0,
      countClassNumber: 0,
      dishNumber: 0
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {
    this.initFn()
  },
  methods: {
    initFn() {
      this.$post(this.api.release_show, {}).then((response) => {
        if (response.code === 0) {
          // release_id：评价id
          // class_id：班级id
          // session_id:年段id
          // dish_id：菜品id
          // is_good：1：好吃 2:不好吃
          this.showId = response.vo.id
          this.dishNumber = response.vo.dish.length
          if (response.vo.dish.length) {
            for (let i in response.vo.dish) {
              let arry_ = {}
              arry_.is_good = 0
              arry_.dish_id = response.vo.dish[i].dish_id
              arry_.pic = response.vo.dish[i].pic
              arry_.name = response.vo.dish[i].name
              this.showCommont.push(arry_)
              this.oldData.push(arry_)
            }
            this.showDetail(response.vo.id, response.vo.dish[0].dish_id)
          }
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    showDetail(releaseId, dish_id) {
      this.$fetch(this.api.release_log_index, {
        release_id: releaseId,
        dish_id: dish_id
      }).then((response) => {
        if (response.code === 0) {
          for (let i in response.list) {
            if (response.list[i].session_id === this.userInfo.section_id && response.list[i].class_id === this.userInfo.class_id) {
              this.countUser = response.list[i].goodNum + response.list[i].notGoodNum
            }
          }
          this.countClassNumber = response.count
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    likecommont(index, option) {
      this.showCommont[index].is_good = option
      let likeNumber = 1
      for (let i in this.showCommont) {
        if (!this.showCommont[i].is_good) {
          likeNumber = 2
        }
      }
      if (likeNumber === 2) {
        console.log('还有未点评')
      } else {
        console.log('点评')
        // this.$alert('评价成功，谢谢', '成功提示', {
        //   confirmButtonText: '确定',
        //   callback: action => {
        //   }
        // });

        // release_id：评价id
        // class_id：班级id
        // session_id:年段id
        // dish_id：菜品id
        // is_good：1：好吃 2:不好吃
        let data_ = []
        for (let i in this.showCommont) {
          let array_ = {}
          array_.is_good = this.showCommont[i].is_good
          array_.dish_id = this.showCommont[i].dish_id
          array_.session_id = this.userInfo.section_id
          array_.class_id = this.userInfo.class_id
          array_.release_id = this.showId
          data_.push(array_)
        }
        let this_ = this
        this.$post(this.api.release_log_add, {
          data: JSON.stringify(data_)
        }).then((response) => {
          if (response.code === 0) {
            // this.$message({
            //   message: '评价成功',
            //   type: 'success'
            // })
            this.countUser += 1
            this.$confirm('评价成功！', '提示', {
              confirmButtonText: '继续点评',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
            }).catch(() => {
            })
            for (let i in this.oldData) {
              console.log(123)
              this_.showCommont[i].is_good = 0
            }
          } else {
          }
        }).catch((err) => {
          console.log(err)
        })
      }
    }
  },
  mounted() {
  }
}
</script>

<style scoped>
.commont-body-star {
  overflow-y: auto;
  height: calc(100vh - 221px);
}
.foodimg-body{
  width: 100%;
  height: 170px;
}
</style>
